package com.koudai.finance.controller;

import com.koudai.finance.entity.User;
import com.koudai.finance.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description: 登录注册模块
 * @author: ls
 * @Date: 2020/7/8 16:12
 */
@Api(tags = "登录注册模块")
@RestController
@RequestMapping("/login")
@Validated
public class LoginController {

    @Resource
    private LoginService loginServiceImpl;

    @ApiOperation(value = "获取微信登录获取回调地址")
    @PostMapping("/getWxAuthUrl")
    public String getWxAuthUrl(String redirectUrl, String type) throws IOException {
        return loginServiceImpl.getWxAuthUrl(redirectUrl, type);
    }

    @ApiOperation(value = "授权获取用户openId")
    @PostMapping("/authWxCallBack")
    public String authWxCallBack(String code, HttpServletResponse response) {
        Assert.hasText(code, "code不能为空");
        return loginServiceImpl.authWxCallBack(code, response);
    }

    @ApiOperation(value = "登录注册")
    @PostMapping("/register")
    public User register(String phone, String smsCode, String openId, HttpServletResponse response) {
        Assert.hasText(phone, "手机号码不能为空");
        Assert.hasText(smsCode, "验证码错误或者为空");
        return loginServiceImpl.register(phone, smsCode, openId, response);
    }

    @ApiOperation(value = "获取短信验证码")
    @PostMapping("/getCode")
    public void getCode(String phone, HttpServletRequest request) {
        Assert.hasText(phone, "手机号码不能为空");
        loginServiceImpl.getVerifyCode(phone, request);
    }

}
